package com.gzy.mylog;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.gzy.mylog.events.LogoutEvent;
import com.gzy.mylog.manager.AuthManager;
import com.gzy.mylog.model.User;
import com.gzy.mylog.modules.fx.FxFragment;
import com.gzy.mylog.modules.live.LiveFragment;
import com.gzy.mylog.modules.log.LogFragment;
import com.gzy.mylog.modules.mine.MineFragment;
import com.gzy.mylog.modules.py.PyFragment;
import com.gzy.mylog.utils.SPUtils;
import com.gzy.mylog.view.TabbarItem;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {

    int[] tabbar_ids = new int[]{R.id.tab_fx,R.id.tab_live,R.id.tab_log,R.id.tab_py,R.id.tab_mine};
    List<TabbarItem> tabbarItemList = new ArrayList<>();

    List<Fragment> fragmentList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.initUI();

        EventBus.getDefault().register(this);
    }

    private void initUI() {

        fragmentList.add(new FxFragment());
        fragmentList.add(new LiveFragment());
        fragmentList.add(new LogFragment());
        fragmentList.add(new PyFragment());
        fragmentList.add(new MineFragment());

        for (int id : tabbar_ids) {
            tabbarItemList.add(findViewById(id));
        }

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        for (Fragment fragment : fragmentList) {
            transaction.add(R.id.rl_content,fragment);
        }
        transaction.commit();


//        tabbarItemList.get(0).setSelected(true);
        selectTabbar(tabbarItemList.get(0));

    }

    public void selectTabbar(View view) {

        TabbarItem view1 = (TabbarItem) view;

        for (TabbarItem item : tabbarItemList) {
            item.setSelected(false);
        }
        view1.setSelected(true);

        int index = tabbarItemList.indexOf(view1);
        Toast.makeText(this, ""+index, Toast.LENGTH_SHORT).show();


        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        for (Fragment fragment : fragmentList) {
            transaction.hide(fragment);
        }
        transaction.show(fragmentList.get(index));
        transaction.commit();

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void xxxxxxxxxx(LogoutEvent event) {
        finish();
    }
}
